﻿@{
    ViewData["Title"] = "修改生产工单";
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>修改生产工单</title>
    <link href="//unpkg.com/layui@2.9.17/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <form class="layui-form layui-form-pane" action="">

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span style="color:red">*</span>工单编号</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <input type="text" name="WorkorderCode" autocomplete="off" placeholder="请输入工单编号" lay-verify="required" class="layui-input" id="WorkorderCode">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width:90px;">自动生成</label>
                <div class="layui-input-inline" style="width: 80px;">
                    <input type="checkbox" name="switch" lay-skin="switch" lay-text="OFF|ON" lay-filter="switchFilter">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span style="color:red">*</span>来源类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="OrderSource" value="ORDER" title="客户订单" id="Y" checked>
                    <input type="radio" name="OrderSource" value="STORE" title="库存备货" id="F">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">单据状态</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <select name="status" lay-filter="aihao" disabled>
                        <option value=""></option>
                        <option selected id="PREPARE" value="PREPARE">草稿</option>
                        <option id="Completed" value="Completed">已完成</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span style="color:red">*</span>工单名称</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <input type="text" name="WorkorderName" autocomplete="off" placeholder="请输入工单名称" lay-verify="required|WorkorderName" class="layui-input" id="WorkorderName">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><span style="color:red">*</span>需求日期</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <input type="text" name="RequestDate" id="RequestDate" lay-verify="required|RequestDate" placeholder="请选择需求日期" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span style="color:red">*</span>工单数量</label>
                <div class="layui-input-inline" style="width: 100px;">
                    <input type="number" id="Quantity" name="Quantity" placeholder="" autocomplete="off" class="layui-input" lay-verify="required" min="0" step="1" lay-affix="number" value="1">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><span style="color:red">*</span>工单类型</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <select name="WorkorderType" lay-filter="aihao">
                        <option value=""></option>
                        <option selected id="SELF" value="SELF">自产</option>
                        <option id="OUTSOURCE" value="OUTSOURCE">外协</option>
                        <option id="PURCHASE" value="PURCHASE">外购</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span style="color:red">*</span>产品编号</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <input type="text" name="ProduceCode" autocomplete="off" placeholder="请选择产品" class="layui-input" id="ProduceCode" lay-verify="required">
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width: 80px;">
                        <button type="button" class="layui-btn layui-btn-normal layui-icon layui-icon-search" id="openWindow">搜索</button>
                    </div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width:120px;">产品名称</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <input type="text" name="ProduceName" autocomplete="off" class="layui-input" id="ProduceName" readonly="readonly">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width:120px;">规格型号</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <input type="text" name="ClientName" autocomplete="off" class="layui-input" id="ClientName" readonly="readonly">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width:120px;">单位</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <input type="text" name="UnifofMeasure" autocomplete="off" class="layui-input" id="UnifofMeasure" readonly="readonly">
                </div>
            </div>

        </div>
          <div class="layui-form-item layui-form-text" hidden>
            <input type="text" name="productId" autocomplete="off" class="layui-input" id="productId" readonly="readonly">
           
       </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" class="layui-textarea" name="remark" id="remark"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn" lay-submit lay-filter="demo3">保存</button>
            <button type="reset" class="layui-btn layui-btn-primary">取消</button>
        </div>
    </form>
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="//unpkg.com/layui@2.9.17/dist/layui.js"></script>
    <script>
            //JavaScript代码
            layui.use(['form'], function () {
                var form = layui.form;
                var layer = layui.layer;
                 var laydate = layui.laydate;
          form.on('switch(switchFilter)', function(data) {
            if(data.elem.checked) {
              var prefix ='MO';
              var num = new Date().getTime(); // 使用当前时间戳生成数字部分
              document.getElementById('WorkorderCode').value = prefix + num;
            } else {
              document.getElementById('WorkorderCode').value = '';
            }
          });

          // 日期
          //日期默认为当前日期
                laydate.render({
                    elem: '#RequestDate',
                    type: 'date',
                    value: new Date(),
                });

           // 供应商选择
        document.getElementById('openWindow').onclick = function(){
            // 弹出新窗口
            layer.open({
                type: 2, // 弹出层类型，2表示iframe层
                title: '物料产品选择',
                  shadeClose: true, // 点击遮罩区域，关闭弹层
                              scrollbar: false, // 暂时屏蔽浏览器滚动条
                    area: ['90%', '90%'], // 弹出层大小
                    content: '/song/MaterialProducts', // 弹出层内容，供应商选择页面地址
                              end: function () {
                        var ItemCode = sessionStorage.getItem('ItemCodeFromPopup');
                        document.getElementById('ProduceCode').value = ItemCode;
                        var ItemName = sessionStorage.getItem('ItemNameFromPopup');
                        document.getElementById('ProduceName').value = ItemName;
                        var UnitOfMeasure = sessionStorage.getItem('UnitOfMeasureFromPopup');
                        document.getElementById('UnifofMeasure').value = UnitOfMeasure;
                        var Specification = sessionStorage.getItem("SpecificationFromPopup");
                        document.getElementById('ClientName').value = Specification;

                           var Specifications = sessionStorage.getItem("FromPopupId");
                        document.getElementById('productId').value = Specifications;
                    }
            });
        };

            function getchildIframeData() {
                var form = layui.form;
                
                $("#Quantity").val(parent.quantity);
                $("#ProduceCode").val(parent.produceCode);
                $("#ProduceName").val(parent.produceName);
                $("#WorkorderCode").val(parent.workorderCode);
                $("#UnifofMeasure").val(parent.unifofMeasure);
                $("#ClientName").val(parent.clientName);
                $("#remark").val(parent.remark);
                $("#WorkorderName").val(parent.workorderName);
                $("#ProductId").val(parent.productId);
                $("#RequestDate").val(parent.requestDate.split("T")[0]);
                if (parent.orderSource == "ORDER") {
                    $("#Y").prop("checked", true);
                } else {
                    $("#F").prop("checked", true);
                }
                form.render();//重新渲染表单
            }
            getchildIframeData()

                // 表单验证
                form.verify({
                    // 入库单名称
                WorkorderName: function (value) {
                        if (value.length > 50) {
                            return '入库单名称不能超过50个字符';
                        }
                    },
                    // 入库日期 验证日期格式
                    RequestDate: function (value) {
                        if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) {
                            return '日期格式不正确，应为yyyy-mm-dd';
                        }
                    },
                });

                // 修改事件
                form.on('submit(demo3)', function (data) {
                    var field = data.field; // 获取表单字段值
                         // 此处可执行 Ajax 等操作
                        console.log(field);
                        var updDTO={
                    // create_By = data.create_By;
                    // creste_Time = data.creste_Time;
                    // id = data.id;
                    // orderSource = data.orderSource;
                    // produceCode = data.produceCode;
                    // produceName = data.produceName;
                    // quantity = data.quantity;
                    // remark = data.remark;
                    // requestDate = data.requestDate;
                    // status = data.status;
                    // unifofMeasure = data.unifofMeasure;
                    // workorderCode = data.workorderCode;
                    // workorderName = data.workorderName;
                    // workorderType = data.workorderType;
                    orderSource: field.OrderSource,
                    produceCode: field.ProduceCode,
                    quantity: field.Quantity,
                    produceName: field.ProduceName,
                    clientName: field.ClientName,
                    remark: field.remark,
                    requestDate: field.RequestDate,
                    id: parent.id,
                    unifofMeasure: field.UnifofMeasure,
                    create_By: parent.create_By,
                    creste_Time: parent.creste_Time,
                    workorderCode: field.WorkorderCode,
                    workorderName: field.WorkorderName,
                    warehouseCode: field.WarehouseCode,
                    workorderType: field.WorkorderType,
                    status: parent.status,
                     productId : field.productId,
                        }
                $.get("/Song/UpdateProWorkorder", updDTO, res => {
                        if (res) {
                            layer.msg('保存成功', {
                                icon: 1,
                                time: 2000
                            }, function () {
                                var index = parent.layer.getFrameIndex(window.name);//先得到当前iframe层的索引
                                parent.layer.close(index);//再执行关闭
                            });
                        }
                        else {
                            layer.msg('网络异常', {
                                icon: 2,
                                time: 2000
                            });
                        }
                    });
                    return false; // 阻止默认 form 跳转
                });
            });
    </script>
</body>
</html>
